package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.dialog.ECAlertDialog;
import com.yuntongxun.ecdemo.common.dialog.ECProgressDialog;
import com.yuntongxun.ecdemo.common.utils.ECPreferenceSettings;
import com.yuntongxun.ecdemo.common.utils.ECPreferences;
import com.yuntongxun.ecdemo.ui.SDKCoreHelper;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.ui.activity.guide.GuideTabActivity;
import com.yuntongxun.ecdemo.voicechat.ui.services.OnlineService;
import com.yuntongxun.ecdemo.voicechat.util.PreferenceUtils;

import java.io.InvalidClassException;

import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 设置
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/21
 */

public class SettingActivity extends BaseVoiceChatActivity {


    @InjectView(R.id.sounds_cb)
    CheckBox soundsCb;
    @InjectView(R.id.vibrate_cb)
    CheckBox vibrateCb;
    @InjectView(R.id.mute_cb)
    CheckBox muteCb;
    @InjectView(R.id.available_cb)
    CheckBox availableCb;
    @InjectView(R.id.busy_cb)
    CheckBox busyCb;
    @OnClick(R.id.modify_psd_layout)
    public void onModifyPsdClick(){
        startActivity(new Intent(this,ModifyPsdActivity.class));
    }

    @OnClick(R.id.about_layout)
    public void onAboutClick(){
        startActivity(new Intent(this,AboutActivity.class));
    }
    @InjectView(R.id.Logout)
    Button Logout;

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initViewsAndEvents() {
        setTitle("Setting");
        registerReceiver(new String[]{SDKCoreHelper.ACTION_LOGOUT});

        String state=PreferenceUtils.getPrefString(SettingActivity.this,Contants.Preference.ONLINE_STATE,Contants.AVAILABLE);
        if (state.equals(Contants.BUSY)){
            busyCb.setChecked(true);
            availableCb.setChecked(false);
        }else{
            availableCb.setChecked(true);
            busyCb.setChecked(false);
        }

        soundsCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                try {
                    ECPreferences.savePreference(ECPreferenceSettings.SETTINGS_NEW_MSG_SOUND, isChecked, true);
                } catch (InvalidClassException e) {
                    Log.e("sound",e.toString());
                }
            }
        });

        vibrateCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                try {
                    ECPreferences.savePreference(ECPreferenceSettings.SETTINGS_NEW_MSG_SHAKE, isChecked, true);
                } catch (InvalidClassException e) {
                    Log.e("shake",e.toString());
                }
            }
        });

        muteCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                soundsCb.setChecked(false);
                vibrateCb.setChecked(false);
            }
        });

        availableCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    busyCb.setChecked(false);
                    PreferenceUtils.setPrefString(SettingActivity.this,Contants.Preference.ONLINE_STATE,Contants.AVAILABLE);
                    PostResult postResult = new PostResult();
                    postResult.setTag(EventBusTags.AVAILABLE);
                    EventBus.getDefault().post(postResult);
                }else{
                    busyCb.setChecked(true);
                    PreferenceUtils.setPrefString(SettingActivity.this,Contants.Preference.ONLINE_STATE,Contants.BUSY);
                    PostResult postResult = new PostResult();
                    postResult.setTag(EventBusTags.BUSY);
                    EventBus.getDefault().post(postResult);
                }
            }
        });

        busyCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    availableCb.setChecked(false);
                    PreferenceUtils.setPrefString(SettingActivity.this,Contants.Preference.ONLINE_STATE,Contants.BUSY);
                    PostResult postResult = new PostResult();
                    postResult.setTag(EventBusTags.BUSY);
                    EventBus.getDefault().post(postResult);
                }else{
                    availableCb.setChecked(true);
                    PreferenceUtils.setPrefString(SettingActivity.this,Contants.Preference.ONLINE_STATE,Contants.AVAILABLE);
                    PostResult postResult = new PostResult();
                    postResult.setTag(EventBusTags.AVAILABLE);
                    EventBus.getDefault().post(postResult);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initSettings();
    }

    /**
     * 初始化
     */
    private void initSettings() {
        initNewMsgNotificationSound();
        initNewMsgNotificationShake();
    }

    /**
     * 初始化新消息声音设置参数
     */
    private void initNewMsgNotificationSound() {
        if(soundsCb == null) {
            return ;
        }
        boolean shakeSetting = ECPreferences.getSharedPreferences().getBoolean(ECPreferenceSettings.SETTINGS_NEW_MSG_SOUND.getId(),
                (Boolean) ECPreferenceSettings.SETTINGS_NEW_MSG_SOUND.getDefaultValue());
        soundsCb.setChecked(shakeSetting);
    }

    /**
     * 初始化新消息震动设置参数
     */
    private void initNewMsgNotificationShake() {
        if(vibrateCb == null) {
            return ;
        }
        boolean shakeSetting = ECPreferences.getSharedPreferences().getBoolean(ECPreferenceSettings.SETTINGS_NEW_MSG_SHAKE.getId(),
                (Boolean) ECPreferenceSettings.SETTINGS_NEW_MSG_SHAKE.getDefaultValue());
        vibrateCb.setChecked(shakeSetting);
    }

    @OnClick(R.id.Logout)
    public void onLogoutClick(){

        ECAlertDialog buildAlert = ECAlertDialog.buildAlert(this, R.string.settings_logout_warning_tip, null, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                mExitType = 0;
                handleLogout(false);
            }

        });
        buildAlert.setTitle(R.string.settings_logout);
        buildAlert.show();



    }
    private int mExitType = 0;
    @Override
    protected void handleReceiver(Context context, Intent intent) {
        super.handleReceiver(context, intent);
        if(SDKCoreHelper.ACTION_LOGOUT.equals(intent.getAction())) {

            try {
                Intent outIntent = new Intent(SettingActivity.this, GuideTabActivity.class);
                outIntent.putExtra(GuideTabActivity.FROME_LOGOUT,"true");
                outIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                if(mExitType == 1) {
                    ECPreferences.savePreference(ECPreferenceSettings.SETTINGS_FULLY_EXIT, true, true);
                    startActivity(outIntent);
                    finish();
                    return ;
                }
                dismissPostingDialog();
                ECPreferences.savePreference(ECPreferenceSettings.SETTINGS_REGIST_AUTO, "", true);
                PreferenceUtils.setPrefString(SettingActivity.this, Contants.Preference.user_info,"");
                startActivity(outIntent);
                finish();

                PostResult postResult = new PostResult();
                postResult.setTag(EventBusTags.LOGOUT);
                EventBus.getDefault().post(postResult);
                intent=new Intent(this, OnlineService.class);
                stopService(intent);
            } catch (InvalidClassException e) {
                e.printStackTrace();
            }
        }
    }

    private ECProgressDialog mPostingdialog;
    /**
     * 处理退出操作
     */
    private void handleLogout(boolean isNotice) {
        mPostingdialog = new ECProgressDialog(this, R.string.posting_logout);
        mPostingdialog.show();
        SDKCoreHelper.logout(isNotice);



    }

    /**
     * 关闭对话框
     */
    private void dismissPostingDialog() {
        if(mPostingdialog == null || !mPostingdialog.isShowing()) {
            return ;
        }
        mPostingdialog.dismiss();
        mPostingdialog = null;
    }

}
